package com.corpgovernment.organization.util;

import com.corpgovernment.common.handler.ICacheHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author: jt.qin
 * @DateTime: 2020/3/5 17:13
 * @Description
 */
@Service
public class TokenValidUtil {
    @Autowired
    private ICacheHandler cacheHandler;

    @Value("${login_time_out:7776000}")
    private long loginTimeOut;
    /**
     * 组织被设置为禁用
     */
    public static final String USER_ORG_DISABLE = "userOrgDisable";
    /**
     * 账号被设置为禁用
     */
    public static final String USER_DISABLE = "userDisable";


    /**
     * 更新用户登录的有效性
     *
     * @param flag true 账号启用 false 账号停用
     */
    public void updateTokenValid(String uid, boolean flag) {
        if (!flag) {
            cacheHandler.setValue(USER_DISABLE + ":" + uid, true, loginTimeOut);//根据token失效时间
        } else {
            cacheHandler.delete(USER_DISABLE + ":" + uid);
        }
    }

    /**
     * 更新用户账号身份的有效性
     *
     * @param flag true 账号启用 false 账号停用
     */
    public void updateUserOrgTokenValid(String uid, String orgId, boolean flag) {
        String key = USER_ORG_DISABLE + ":" + uid + "-" + orgId;
        if (!flag) {
            cacheHandler.setValue(key, true, loginTimeOut);//根据token失效时间
        } else {
            cacheHandler.delete(key);
        }
    }
}
